package org.wangduqing.ibatisdao.spring.map;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

public class IbatisDaoLinkedMap extends LinkedHashMap<String, Object> {

	private static final long serialVersionUID = 4707118098163839208L;
	private final Locale locale;
	
	public IbatisDaoLinkedMap(Locale locale){
		
		this.locale = locale == null ? Locale.getDefault() : locale;
		
	}
	
	public IbatisDaoLinkedMap(){
		
		this(((Locale)(null)));
		
	}
	
	public IbatisDaoLinkedMap(int initialCapacity, Locale locale){
		
		super(initialCapacity);
		this.locale = locale == null ? Locale.getDefault() : locale; 
		
	}
	
	public IbatisDaoLinkedMap(int initialCapacity){
		
		this(initialCapacity, null);
		
	}
	
	protected String convertKey(String key){
		
		return key.toLowerCase(locale);
		
	}
	
	public Object put(String key, Object value){
		
		return super.put(convertKey(key), value);
		
	}
	
	public void putAll(Map map){
		
		if(map.isEmpty()){
			
			return;
			
		}
		java.util.Map.Entry entry;
		for(Iterator iterator=map.entrySet().iterator();iterator.hasNext();put(convertKey((String)entry.getKey()),entry.getValue())){
			
			entry = (java.util.Map.Entry)iterator.next();
			
		}
		
	}
	 
	public boolean containsKey(Object key){
		
		return (key instanceof String) && super.containsKey(convertKey((String)key));
		
	}
	
	public Object get(Object key){
		
		if(key instanceof String){
			
			return super.get(convertKey((String)key));
			
		}else{
			
			return null;
			
		}
		
	}
	
	public Object remove(Object key){
		
		if(key instanceof String){
			
			return super.remove(convertKey((String)key));
			
		}else{
			
			return null;
			
		}
		
	}
	
	public void clear(){
		
		super.clear();
		
	}
	
}
